package com.lpj;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CallSumReducer extends Reducer<Text,CallBean,Text,CallBean> {
    //框架每传递一组数据<手机号,{CallBean,CallBean,CallBean...}>调用一次我们的reduce方法
    //reduce中的业务逻辑就是遍历values，然后累加求和再输出
    @Override
    protected void reduce(Text key, Iterable<CallBean> values, Context context) throws IOException, InterruptedException {
        //打出去的电话计数器和打进来的电话计数器
        long call_out = 0;
        long call_in = 0;
        //打出去的电话和打进来的电话累加求和
        for (CallBean callBean : values) {
            call_out += callBean.getCallOut();
            call_in += callBean.getCallIn();
        }
        //将结果输出
        context.write(key, new CallBean(key.toString(), call_out, call_in));
    }
}